<?php

    require "config.php"; // NOTE:It's a best practice to keep your private information in different file which has access to limited people

    $con = mysql_connect("localhost",$db_user ,$db_pass);
    error_reporting(0);	
    // if an error was encountered, the script exits with an error message
    if (!$con){
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db($db_name, $con); //connecting with the database
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Search Course Data</title>
</head>
<style>
html {
    overflow: scroll; 
}
.scrollable{
   overflow: auto;
   width: 70px; /* adjust this width depending to amount of text to display */
   height: 80px; /* adjust height depending on number of options to display */
   border: 1px silver solid;
}

body{background-image:url('Images/sjsu logo.jpg');
background-repeat: no-repeat;
  background position: top center;
  background-size: 100%;
  background-size: 1400px 1200px;
  position:relative;}
  
h3.pos_fixed{
    color:#6D63A9;
    position:relative;
    top:25px;
    left:30px;
}

#b{
    position:relative;
    top:140px;
    left:320px;
}

a:link    {
    /* Applies to all unvisited links */
    text-decoration:  none;
    font-weight:      bold;
    color:          "#blue";
    font-size:120%;
} 

</style>

<div id="b">
   <table border='1'>
   <tr> 
   </tr>
<div id = "e">
<? 
    $selection = 0;

    foreach($_POST['select'] as $select){
	$selection = $selection + 1;
	list ($cnum, $csec, $cid, $cterm, $sid) = explode(":", $select);
    }
    //$fetch_count = "SELECT COUNT(studentId) as studentCount FROM CourseSection WHERE courseId IN (SELECT courseId  FROM CourseSection  WHERE courseNumber = '$_POST[courseNumber]' AND courseSection)";
    if ( $selection > 1 ){
        echo "ERROR: Only 1 section per course can be enrolled.<br";
    } else {
	$insert_data = "INSERT INTO Enrollment (studentId, courseSection, courseId, currentTerm) VALUES ($sid, '$csec', $cid, '$cterm')";
	$result = mysql_query($insert_data); 
	echo "Course enrolled successfully.<br>";
    }

   // $num_rows = mysql_num_rows($count);
   /*if ($num_rows == 0) {
	echo "Course does not exist <br> <br> <br>";
   }
	while($row = mysql_fetch_array($count)){
	    echo "<tr>";
	    if ($row['studentCount'] > 45) {
		echo " Seat is unavailable <br>";
	    } else {
                mysql_query($insert_data);
		echo "Course Enrolled Successfully <br>";
	    }
   	}*/
   mysql_close($con);
?>
  </table>
<div id="c">
<div id="d"><br><br><a href="MainMenu.php">Home</a>
</body>
</html>
